export const fileFormat = (info) => {
  const arr = info ? info.split(",") : [];
  if (arr.length > 0) {
    const srcList = arr.map((item) => {
      return `/api/cbb-hzz/file/not-auth/download/${item}`;
    });
    return srcList;
  } 
    return [];
  
};
export const onlyFileIds = (info) => {
  // 处理文件：编辑文件时，若不对文件进行任何操作，原样提交，会导致文件提交内容为后端返回数组对象，不符合文件提交要求，需只提出id
  if (info.length > 0) {
    const srcList = info.map((item) => {
      return item.id;
    });
    return srcList.toString();
  } 
    return "";
  
};
export const fileSize = (value) => {
  if (value === null || value === "") {
    return "0 Bytes";
  }
  const unitArr = ["Bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"];
  let index = 0;
  const srcsize = parseFloat(value);
  index = Math.floor(Math.log(srcsize) / Math.log(1024));
  let size = srcsize / Math.pow(1024, index);
  size = size.toFixed(2); //保留的小数位数

  return size + unitArr[index];
};
export default {
  fileFormat,
  fileSize,
  onlyFileIds
};
// 根据后缀判断文件文件类型
export const getFileType = (fileName) =>{
  // 后缀获取
  let suffix = '';
  // 获取类型结果
  let result = '';
  try {
    const flieArr = fileName.split('.');
    suffix = flieArr[flieArr.length - 1];
  } catch (err) {
    suffix = '';
  }
  // fileName无后缀返回 false
  if (!suffix) { return false; }
  suffix = suffix.toLocaleLowerCase();
  // 图片格式
  const imglist = ['png', 'jpg', 'jpeg', 'bmp', 'gif'];
  // 进行图片匹配
  result = imglist.find(item => item === suffix);
  if (result) {
    return 'image';
  }
  // 匹配 pdf
  const pdflist = ['pdf'];
  result = pdflist.find(item => item === suffix);
  if (result) {
    return 'pdf';
  }
  // 匹配 视频
  const videolist = ['mp4', 'm2v', 'mkv', 'rmvb', 'wmv', 'avi', 'flv', 'mov', 'm4v'];
  result = videolist.find(item => item === suffix);
  if (result) {
    return 'video';
  }
  // 匹配 音频
  const radiolist = ['mp3', 'wav', 'wmv'];
  result = radiolist.find(item => item === suffix);
  if (result) {
    return 'radio';
  }
  // 其他 文件类型
  return 'file';
}
